//
//  VisitorView.swift
//  XMGWB
//
//  Created by J on 16/2/22.
//  Copyright © 2016年 xiaomage. All rights reserved.
//

import UIKit
//Swift中如何定义协议:必须遵守NSObjectProtocol
protocol VisitorViewDelegate:NSObjectProtocol{
    
    //登陆回调
    func loginButtonClick()
    //注册回调
    func regesterButtonClick()

}


class VisitorView: UIView {
    
    //定义一个属性保存代理对象 一定要加上weak避免循环引用
   weak var delegate:VisitorViewDelegate?
    
    ///  设置未登陆界面
    ///
    ///  - parameter isHome:    是否是首页
    ///  - parameter imageName: 需要展示的图标名称
    ///  - parameter message:   需要展示的文本内容
    func setUpVisitorView(isHome:Bool,imageName:String,message:String){
        
        //如果不是首页,隐藏转盘
        iconView.hidden = !isHome
        //修改中间的图标
        homeIcon.image = UIImage(named: imageName)
        //修改文本
        messageLabel.text = message
        //判断是否需要执行动画
        if isHome{
            starAnimation()
        }
        
    }
    
    func setupVisitorInfo(isHome: Bool, imageName: String, message: String){
        iconView.hidden = !isHome
        messageLabel.text = message
        homeIcon.image = UIImage(named: imageName)
        
       
    }
    
    
    func loginButtonClick(btn:UIButton){
        print(#function)
        delegate?.loginButtonClick()//Swift中不用做判断 前面没有值后面直接没有效果
    }
    
    
    func regesterButtonClick(btn:UIButton){
        delegate?.regesterButtonClick()
        print(#function)
    }
 
   override init(frame: CGRect) {
        super.init(frame: frame)
        
        //1.添加子控件
    addSubview(iconView)
    addSubview(maskBGView)    
    addSubview(homeIcon)
    addSubview(messageLabel)
    addSubview(loginButton)
    addSubview(regesterButton)

        //2.布局子控件
        //2.1设置背景
    iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
    //2.2设置小房子
    homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
    //2.3设置文本
    messageLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
   let iconWidth = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224)
    addConstraint(iconWidth)
    
    //2.4设置注册按钮
    regesterButton.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 30),offset: CGPoint(x: 0, y: 20))//y轴偏移20
    
    //2.5设置login按钮
    loginButton.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 30),offset: CGPoint(x: 0, y: 20))//y轴偏移20

    //设置蒙板
    maskBGView.xmg_Fill(self)//填充整个控制器View
    
    
    }
    
    
    //Swift推荐我们自定义一个控件,要么用纯代码,要么用Xib/StoryBoard
    
  required  init?(coder aDecoder: NSCoder) {
    //如果用Xib/StoryBoard 创建改类那么就会崩溃
    super.init(coder: aDecoder)
    
    
    
    
    
    }
    
    //MARK:内部控制方法
    private func starAnimation(){
        //1.创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //2.设置动画属性
        anim.toValue = 2*M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        //该属性默认为YES,代表动画执行完毕就移除
        anim.removedOnCompletion = false
        //3.将动画添加到图层上
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }

    
    
    //MARK:懒加载控件
    /// 转盘
    private lazy var iconView:UIImageView = {
       let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
    
    /// 房子图标
    private lazy var homeIcon:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    
    /// 文本
    private lazy var messageLabel :UILabel = {
        
        let label = UILabel()
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        return label
        
    }()
    
    /// 登陆按钮
    private lazy var loginButton :UIButton = {
        
        let login = UIButton()
        login.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        login.setTitle("登录", forState: UIControlState.Normal)
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        login.addTarget(self, action: #selector(VisitorView.loginButtonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        return login
        
    }()
    
    /// 注册按钮
    private lazy var regesterButton :UIButton = {
        
        let regester = UIButton()
        regester.setTitle("注册", forState: UIControlState.Normal)
//        print(regester.titleColorForState(UIControlState.Normal))//默认白色
        regester.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        regester.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        regester.addTarget(self, action: #selector(VisitorView.regesterButtonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        return regester
        
    }()
    
    
    //蒙板imageView
    private lazy var maskBGView:UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
    }()
    
    
    
    
}
